package com.guigu.www.erp.dengtao.controller;

import com.guigu.www.erp.common.pojo.*;
import com.guigu.www.erp.dehao.pojo.User;
import com.guigu.www.erp.dengtao.service.DModuleDetailsService;
import com.guigu.www.erp.dengtao.service.MDesignProcedureDetailsService;
import com.guigu.www.erp.dengtao.service.MDesignProcedureModuleService;
import com.guigu.www.erp.dengtao.service.MDesignProcedureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/gxwl")
public class GxWliao {
    @Autowired
    MDesignProcedureService mDesignProcedureService;
    @Autowired
    MDesignProcedureDetailsService mDesignProcedureDetailsService;
    @Autowired
    DModuleDetailsService dModuleDetailsService;
    @Autowired
    MDesignProcedureModuleService mDesignProcedureModuleService;

    @PostMapping("gxwlsjd")
    public PageVo<MDesignProcedure> gxwl(@RequestBody Map<String, String> map) {
        return mDesignProcedureService.gxwlsjd(map);
    }

    @PostMapping("gxmxwlsj/{id}")
    public List<MDesignProcedureDetails> gxmxwl(@PathVariable String id) {
        return mDesignProcedureDetailsService.gxwlsj(id);
    }

    @PostMapping("cpgxwlsj/{id}")
    public List<DModuleDetails> cpgxwlsj(@PathVariable String id) {
        //System.out.println(id);
        return dModuleDetailsService.cpgxsj(id);
    }

    @PostMapping("addcpwlmx")
    public void add(@RequestBody MDesignProcedureModule mDesignProcedureModule) {
        Float subtotal = mDesignProcedureModule.getSubtotal();
        Integer parentId = mDesignProcedureModule.getParentId();
        MDesignProcedureDetails m = new MDesignProcedureDetails();
        m.setModuleSubtotal(subtotal);
        m.setId(parentId);
        m.setDesignModuleTag("1");
        mDesignProcedureDetailsService.up(m);
        //获取值减数量
        Integer amount = mDesignProcedureModule.getAmount();
        String productId = mDesignProcedureModule.getProductId();
        dModuleDetailsService.up(amount, productId);
        mDesignProcedureModule.setSubtotal(Float.parseFloat(mDesignProcedureModule.getType()));
        mDesignProcedureModuleService.add(mDesignProcedureModule);
    }

    @PutMapping("/putgxwl")
    public Integer xggxwl(@RequestBody MDesignProcedure mDesignProcedure) {
        mDesignProcedure.setDesignModuleTag("1");

        mDesignProcedureService.upt(mDesignProcedure);
        List<MDesignProcedureDetails> list = mDesignProcedure.getList();
        for (int i = 0; i < list.size(); i++) {
            MDesignProcedureDetails m = list.get(i);
            m.setDesignModuleTag("1");
            mDesignProcedureDetailsService.upd(m);
        }
        return 0;
    }

    @PostMapping("gxwlsh")
    public PageVo<MDesignProcedure> gxwlsh(@RequestBody Map<String, String> map, HttpServletRequest request) {
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        if (loginUser.getRole().getRoleId()==4){
            return null;
        }
        return mDesignProcedureService.gxwlsh(map);

    }

    @PostMapping("gxwlmx/{id}")
    public MDesignProcedureDetails gxwlmxcx(@PathVariable String id) {
        return mDesignProcedureDetailsService.selectAll(id);
    }

    @PostMapping("scwlmx/{id}")
    public List<MDesignProcedureModule> scwlmx(@PathVariable String id) {
        return mDesignProcedureModuleService.seleAll(id);
    }

    @PutMapping("gxwltg/{id}")
    public Integer gxwltg(@PathVariable String id) {
        mDesignProcedureService.upttg(Integer.parseInt(id));
        return 0;
    }

    @PutMapping("gxwlbtg/{id}")
    public Integer gxwlbtg(@PathVariable String id) {
        mDesignProcedureService.upbtg(Integer.parseInt(id));
        return 0;
    }

    @PostMapping("gxwlsjdcx")
        public PageVo<MDesignProcedure> gxwlsjdcx(@RequestBody Map<String, String> map) {
        return mDesignProcedureService.sjdcxsy(map);
    }

    @PostMapping("gxwlsjdbg")
    public PageVo<MDesignProcedure> gxwlsjdbg(@RequestBody Map<String, String> map) {
        return mDesignProcedureService.sjdbg(map);
    }

    @PostMapping("cxcpmx/{id}")
    public List<MDesignProcedureModule> cxcpmx(@PathVariable String id) {
        return mDesignProcedureModuleService.seleAll(id);
    }

    @PutMapping("xg")
    public void up(@RequestBody MDesignProcedureDetails m) {
        mDesignProcedureDetailsService.up(m);
    }

    @PutMapping("xgsl")
    public void xgsl(@RequestBody MDesignProcedureModule mDesignProcedureModule) {
        mDesignProcedureModuleService.up(mDesignProcedureModule);
    }

    @PutMapping("xgzt/{id}")
    public int  xgzt(@PathVariable String id) {
        mDesignProcedureService.xg(id);
        return 1;
    }


    @PostMapping("cxck/{id}")
    public MDesignProcedure cxck(@PathVariable String id){
      return   mDesignProcedureService.cxck(Integer.parseInt(id));
    }

    @PostMapping("cxckx/{id}")
    public List<MDesignProcedureDetails> cxckx(@PathVariable String id){
        //System.out.println(mDesignProcedureDetailsService.cxckx(Integer.parseInt(id)));
        return   mDesignProcedureDetailsService.cxckx(Integer.parseInt(id));
    }

    @PostMapping("ckxwy/{id}")
    public List<MDesignProcedureModule> ckxwy(@PathVariable String id){
        //System.out.println(mDesignProcedureModuleService.ck(Integer.parseInt(id)));
        return  mDesignProcedureModuleService.ck(Integer.parseInt(id));
    }
}
